<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>Es6 | 异步,导出导入,编译es5详解 | 一只咸鱼 | 努力学习最新姿势！！！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="web">
    <meta name="description" content="ES6ES6， 全称 ECMAScript 6.0 ，是 JavaScript 的下一个版本标准，2015.06 发版。 ES6 主要是为了解决 ES5 的先天不足，比如 JavaScript 里并没有类的概念，但是目前浏览器的 JavaScript 是 ES5 版本，大多数高版本的浏览器也支持 ES6，不过只实现了 ES6 的部分特性和功能。 async和await这里讲一下async和awai">
<meta property="og:type" content="article">
<meta property="og:title" content="Es6 | 异步,导出导入,编译es5详解">
<meta property="og:url" content="http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/index.html">
<meta property="og:site_name" content="一只咸鱼">
<meta property="og:description" content="ES6ES6， 全称 ECMAScript 6.0 ，是 JavaScript 的下一个版本标准，2015.06 发版。 ES6 主要是为了解决 ES5 的先天不足，比如 JavaScript 里并没有类的概念，但是目前浏览器的 JavaScript 是 ES5 版本，大多数高版本的浏览器也支持 ES6，不过只实现了 ES6 的部分特性和功能。 async和await这里讲一下async和awai">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-06T09:11:00.000Z">
<meta property="article:modified_time" content="2020-04-30T12:08:42.000Z">
<meta property="article:author" content="Mr.Zhang">
<meta property="article:tag" content="web">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="一只咸鱼" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Mr.Zhang</h5>
          <a href="mailto:zhangticcc@163.com" title="zhangticcc@163.com" class="mail">zhangticcc@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                首页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                时间轴
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5392412243" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                微博
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhangti0708" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                开源地址
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://blog.csdn.net/weixin_40902527" target="_blank" >
                <i class="icon icon-lg icon-rss"></i>
                技术博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://zhangticcc.gitee.io/webgis/" target="_blank" >
                <i class="icon icon-lg icon-globe"></i>
                GIS案例
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://mp.weixin.qq.com/s/p4TOhKcr4RZkZWzuMYyBEQ" target="_blank" >
                <i class="icon icon-lg icon-weixin"></i>
                微信订阅号
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Es6 | 异步,导出导入,编译es5详解</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Es6 | 异步,导出导入,编译es5详解</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-04-06T09:11:00.000Z" itemprop="datePublished" class="page-time">
  2020-04-06
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#ES6"><span class="post-toc-number">1.</span> <span class="post-toc-text">ES6</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#async和await"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">async和await</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#async"><span class="post-toc-number">1.1.1.</span> <span class="post-toc-text">async</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#await"><span class="post-toc-number">1.1.2.</span> <span class="post-toc-text">await</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#export"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">export</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#import"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">import</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#babel"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">babel</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-Es6-导入导出和编译es5"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Es6 | 异步,导出导入,编译es5详解</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-04-06 17:11:00" datetime="2020-04-06T09:11:00.000Z"  itemprop="datePublished">2020-04-06</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h3 id="ES6"><a href="#ES6" class="headerlink" title="ES6"></a>ES6</h3><p><code>ES6， 全称 ECMAScript 6.0 ，是 JavaScript 的下一个版本标准，2015.06 发版。
ES6 主要是为了解决 ES5 的先天不足，比如 JavaScript 里并没有类的概念，但是目前浏览器的 JavaScript 是 ES5 版本，大多数高版本的浏览器也支持 ES6，不过只实现了 ES6 的部分特性和功能。</code></p>
<h4 id="async和await"><a href="#async和await" class="headerlink" title="async和await"></a>async和await</h4><p>这里讲一下async和await虽然是es7新出的但是用的比较多。<br>关于异步处理，ES5的回调使我们陷入地狱，ES6的Promise使我们脱离魔障，终于、ES7的async-await带我们走向光明。现在就看一下async-await</p>
<p>基本语法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">async function basicDemo() &#123;</span><br><span class="line">    let result &#x3D; await Math.random();</span><br><span class="line">    console.log(result);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">basicDemo();</span><br></pre></td></tr></table></figure>

<h5 id="async"><a href="#async" class="headerlink" title="async"></a>async</h5><p>async用来表示函数是异步的，定义的函数会返回一个promise对象，可以使用then方法添加回调函数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">async function demo01() &#123;</span><br><span class="line">    return 123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">demo01().then(val &#x3D;&gt; &#123;</span><br><span class="line">    console.log(val);&#x2F;&#x2F; 123</span><br><span class="line">&#125;);</span><br><span class="line">若 async 定义的函数有返回值，return 123;相当于Promise.resolve(123),没有声明式的 return则相当于执行了Promise.resolve();</span><br></pre></td></tr></table></figure>

<h5 id="await"><a href="#await" class="headerlink" title="await"></a>await</h5><p>await 可以理解为是 async wait 的简写。await 必须出现在 async 函数内部，不能单独使用。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function notAsyncFunc() &#123;</span><br><span class="line">    await Math.random();</span><br><span class="line">&#125;</span><br><span class="line">notAsyncFunc();</span><br></pre></td></tr></table></figure>
<p>await 后面可以跟任何的JS 表达式。虽然说 await 可以等很多类型的东西，但是它最主要的意图是用来等待 Promise 对象的状态被 resolved。如果await的是 promise对象会造成异步函数停止执行并且等待 promise 的解决,如果等的是正常的表达式则立即执行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">function sleep(second) &#123;</span><br><span class="line">    return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">        setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            resolve(&#39; enough sleep~&#39;);</span><br><span class="line">        &#125;, second);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line">function normalFunc() &#123;</span><br><span class="line">    console.log(&#39;normalFunc&#39;);</span><br><span class="line">&#125;</span><br><span class="line">async function awaitDemo() &#123;</span><br><span class="line">    await normalFunc();</span><br><span class="line">    console.log(&#39;something, ~~&#39;);</span><br><span class="line">    let result &#x3D; await sleep(2000);</span><br><span class="line">    console.log(result);&#x2F;&#x2F; 两秒之后会被打印出来</span><br><span class="line">&#125;</span><br><span class="line">awaitDemo();</span><br><span class="line">&#x2F;&#x2F; normalFunc</span><br><span class="line">&#x2F;&#x2F; VM4036:13 something, ~~</span><br><span class="line">&#x2F;&#x2F; VM4036:15  enough sleep~</span><br></pre></td></tr></table></figure>

<h4 id="export"><a href="#export" class="headerlink" title="export"></a>export</h4><p>在es6中一个文件可以默认为一个模块，模块通过export向外暴露接口，实现模块间交互等功能</p>
<p>相关语法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">export var m &#x3D; 1;</span><br><span class="line">&#x2F;&#x2F; 等价于</span><br><span class="line">var m &#x3D; 1;</span><br><span class="line">export &#123; m &#125;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; 导出多个</span><br><span class="line">var a &#x3D; 1;</span><br><span class="line">var b &#x3D; 2;</span><br><span class="line">var c &#x3D; 3;</span><br><span class="line">export &#123; a, b, c &#125;</span><br><span class="line">&#x2F;&#x2F; 导出对象</span><br><span class="line">export const student &#x3D; &#123;</span><br><span class="line">  name: &#39;Megan&#39;,</span><br><span class="line">  age: 18</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F; 导出函数</span><br><span class="line">export function add(a, b) &#123;</span><br><span class="line">  return a + b;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;导出接口别名</span><br><span class="line">const person &#x3D; &#123;</span><br><span class="line">  name: &#39;张呆&#39;,</span><br><span class="line">  age: 18,</span><br><span class="line">  gender: &quot;male&quot;</span><br><span class="line">&#125;</span><br><span class="line">export &#123; person as boy &#125;</span><br></pre></td></tr></table></figure>

<p>export default</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">export default var i &#x3D; 0;</span><br><span class="line"></span><br><span class="line">export导出语法</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; default exports</span><br><span class="line">export default 42;</span><br><span class="line">export default &#123;&#125;;</span><br><span class="line">export default [];</span><br><span class="line">export default foo;</span><br><span class="line">export default function () &#123;&#125;</span><br><span class="line">export default class &#123;&#125;</span><br><span class="line">export default function foo () &#123;&#125;</span><br><span class="line">export default class foo &#123;&#125;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; variables exports</span><br><span class="line">export var foo &#x3D; 1;</span><br><span class="line">export var foo &#x3D; function () &#123;&#125;;</span><br><span class="line">export var bar; &#x2F;&#x2F; lazy initialization</span><br><span class="line">export let foo &#x3D; 2;</span><br><span class="line">export let bar; &#x2F;&#x2F; lazy initialization</span><br><span class="line">export const foo &#x3D; 3;</span><br><span class="line">export function foo () &#123;&#125;</span><br><span class="line">export class foo &#123;&#125;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; named exports</span><br><span class="line">export &#123;foo&#125;;</span><br><span class="line">export &#123;foo, bar&#125;;</span><br><span class="line">export &#123;foo as bar&#125;;</span><br><span class="line">export &#123;foo as default&#125;;</span><br><span class="line">export &#123;foo as default, bar&#125;;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; exports from</span><br><span class="line">export * from &quot;foo&quot;;</span><br><span class="line">export &#123;foo&#125; from &quot;foo&quot;;</span><br><span class="line">export &#123;foo, bar&#125; from &quot;foo&quot;;</span><br><span class="line">export &#123;foo as bar&#125; from &quot;foo&quot;;</span><br><span class="line">export &#123;foo as default&#125; from &quot;foo&quot;;</span><br><span class="line">export &#123;foo as default, bar&#125; from &quot;foo&quot;;</span><br><span class="line">export &#123;default&#125; from &quot;foo&quot;;</span><br><span class="line">export &#123;default as foo&#125; from &quot;foo&quot;;</span><br></pre></td></tr></table></figure>

<h4 id="import"><a href="#import" class="headerlink" title="import"></a>import</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">import导入语法</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; default imports</span><br><span class="line">import foo from &quot;foo&quot;;</span><br><span class="line">import &#123;default as foo&#125; from &quot;foo&quot;;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; named imports</span><br><span class="line">import &#123;bar&#125; from &quot;foo&quot;;</span><br><span class="line">import &#123;bar, baz&#125; from &quot;foo&quot;;</span><br><span class="line">import &#123;bar as baz&#125; from &quot;foo&quot;;</span><br><span class="line">import &#123;bar as baz, xyz&#125; from &quot;foo&quot;;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; glob imports</span><br><span class="line">import * as foo from &quot;foo&quot;;</span><br><span class="line"> </span><br><span class="line">&#x2F;&#x2F; mixing imports</span><br><span class="line">import foo, &#123;baz as xyz&#125; from &quot;foo&quot;;</span><br><span class="line">import * as bar, &#123;baz as xyz&#125; from &quot;foo&quot;;</span><br><span class="line">import foo, * as bar, &#123;baz as xyz&#125; from &quot;foo&quot;;</span><br></pre></td></tr></table></figure>

<p>示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">注意事项</span><br><span class="line">导入语句只能作为模块顶层的语句出现，不能出现在 function 里面或是 if 里面</span><br><span class="line"></span><br><span class="line">if(Math.random()&gt;0.5)&#123;</span><br><span class="line">  import &#39;.&#x2F;module1.js&#39;; &#x2F;&#x2F; SyntaxError: Unexpected keyword &#39;import&#39;</span><br><span class="line">&#125;</span><br><span class="line">const import2 &#x3D; (import &#39;.&#x2F;main2.js&#39;); &#x2F;&#x2F; SyntaxError</span><br><span class="line">try&#123;</span><br><span class="line">  import &#39;.&#x2F;module3.js&#39;; &#x2F;&#x2F; SyntaxError: Unexpected keyword &#39;import&#39;</span><br><span class="line">&#125;catch(err)&#123;</span><br><span class="line">  console.error(err);</span><br><span class="line">&#125;</span><br><span class="line">const moduleNumber &#x3D; 4;</span><br><span class="line">import module4 from &#96;module$&#123;moduleNumber&#125;&#96;; &#x2F;&#x2F; SyntaxError: Unexpected token</span><br><span class="line"> </span><br><span class="line">import 语句会被提升到文件顶部执行，也就是说在模块初始化的时候所有的 import 都必须已经导入完成</span><br><span class="line">import &#39;.&#x2F;module1.js&#39;;</span><br><span class="line">alert(&#39;code1&#39;);</span><br><span class="line">import module2 from &#39;.&#x2F;module2.js&#39;;</span><br><span class="line">alert(&#39;code2&#39;);</span><br><span class="line">import module3 from &#39;.&#x2F;module3.js&#39;;</span><br></pre></td></tr></table></figure>

<h4 id="babel"><a href="#babel" class="headerlink" title="babel"></a>babel</h4><p>初始化工程 <code>npm init</code></p>
<p>配置.balbelrc文件添加</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;presets&quot;: [],</span><br><span class="line">  &quot;plugins&quot;: []</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>安装依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"># ES2015转码规则</span><br><span class="line">$ npm install --save-dev babel-preset-es2015</span><br><span class="line"> </span><br><span class="line"># react转码规则</span><br><span class="line">$ npm install --save-dev babel-preset-react</span><br><span class="line"> </span><br><span class="line"># ES7不同阶段语法提案的转码规则（共有4个阶段），选装一个</span><br><span class="line">$ npm install --save-dev babel-preset-stage-0</span><br><span class="line">$ npm install --save-dev babel-preset-stage-1</span><br><span class="line">$ npm install --save-dev babel-preset-stage-2</span><br><span class="line">$ npm install --save-dev babel-preset-stage-3</span><br></pre></td></tr></table></figure>

<p>将配置的规则加入.balbelrc</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;presets&quot;: [</span><br><span class="line">      &quot;es2015&quot;,</span><br><span class="line">      &quot;react&quot;,</span><br><span class="line">      &quot;stage-2&quot;</span><br><span class="line">    ],</span><br><span class="line">    &quot;plugins&quot;: []</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>

<p>安装babel-cli工具</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save-dev babel-cli</span><br></pre></td></tr></table></figure>

<p>转换</p>
<p><code>npm run build</code></p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-04-30T12:08:42.000Z" itemprop="dateUpdated">2020-04-30 20:08:42</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/" target="_blank" rel="external">http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/</a>
        
    </div>
    
    <footer>
        <a href="http://yoursite.com">
            <img src="/img/avatar.jpg" alt="Mr.Zhang">
            Mr.Zhang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/web/" rel="tag">web</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/&title=《Es6 | 异步,导出导入,编译es5详解》 — 一只咸鱼&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/&title=《Es6 | 异步,导出导入,编译es5详解》 — 一只咸鱼&source=分享各种福利和姿势..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Es6 | 异步,导出导入,编译es5详解》 — 一只咸鱼&url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2020/04/06/ol-%E5%9F%BA%E4%BA%8Eol-es6-node-webpack%E7%9A%84%E5%BC%80%E5%8F%91%E6%A8%A1%E7%89%88/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">ol| 基于ol+es6+webpack的开发模版</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2020/04/06/node-%E5%9F%BA%E4%BA%8Enode%E7%9A%84%E4%BA%91%E5%87%BD%E6%95%B0%E7%88%AC%E8%99%AB/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">node|基于node的爬虫服务</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Mr.Zhang &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/&title=《Es6 | 异步,导出导入,编译es5详解》 — 一只咸鱼&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/&title=《Es6 | 异步,导出导入,编译es5详解》 — 一只咸鱼&source=分享各种福利和姿势..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Es6 | 异步,导出导入,编译es5详解》 — 一只咸鱼&url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://yoursite.com/2020/04/06/Es6-%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%92%8C%E7%BC%96%E8%AF%91es5/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
